package cn.nami.web.admin;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.nami.web.frontpage.jdo.VisitorJdo;
import cn.nami.web.util.StringUtil;

@SuppressWarnings("serial")
public class VisitorServlet extends HttpServlet
{
	private final String DELETE = "delete";

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String method = request.getParameter("method");
		String order = request.getParameter("order");
		if(DELETE.equals(method))
		{
			delete(request, response);
		}
		else
		{
			request.setAttribute("visitorList", VisitorJdo.listAllVisitors(order));
			request.setAttribute("order", order);
			request.getRequestDispatcher("/admin/listVisitor.jsp").forward(request, response);
		}
	}

	
	public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{ 
		String id = request.getParameter("id");
		if(StringUtil.isInteger(id))
		{
			VisitorJdo.deleteById(Integer.parseInt(id));
		}
		response.sendRedirect(request.getContextPath() + "/admin/VisitorServlet");
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		doGet(request, response);
	}
}